import { createRouter, createMemoryHistory } from 'vue-router'
// createRouter 创建路由的实例对象
// createMemoryHistory 用于指定路由的工作模式 hash
const router = createRouter({
  history: createMemoryHistory(),

  routes: [
    { path: '/', component: () => import('../components/test.vue'), redirect: '/MyTest' }, // redirect 表示重定向
    { path: '/MyTest', component: () => import('../components/test.vue') },
    { path: '/MyTest1', component: () => import('../components/test1.vue') },
    { path: '/MyTest2', component: () => import('../components/test2.vue') },
    { path: '/move/:id', component: () => import('../components/move.vue'), props: true }, // 动态参数
  ]
})
// localStorage.setItem('token', 'Bearer xxx')
localStorage.clear()
// 导航守卫
// 全局导航守卫 to 表示去往的路由， from 表示从哪里前往, next 表示放行
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  console.log(to.fullPath, from.fullPath)
  // next() 表示路由放行
  // if(to.fullPath === '/MyTest') return next('/MyTest1')  // 表示除了 MyTest 组件，其他组件都能访问
  if(to.fullPath === '/MyTest1' && token) {
    console.log(token)
    next()
    return
  }
  next()
})

export default router
